/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package iteso.compiladores.logo;

import java.awt.Color;
import java.util.Hashtable;
import java.util.Vector;


/**
 *
 * @author armandomolina
 */
public class Helper {
    
        private static boolean tempBoolean = false;
	
	public static boolean getTempBoolean(){
		return tempBoolean;
	}
	
	public static void setTempBoolean(boolean bol){
		tempBoolean = bol;
	}

	public static final int centerX = 375;
	public static final int centerY = 200;
	
	public static Color penColor = Color.black;
	public static boolean penDown = true;
	public static int penSize = 0;
	
	private static int angleMegaman = 0;
	private static String output = "";
	//lineas a dibujar
	private static Vector<Coordenadas> coordenadas = new Vector<Coordenadas>();
	//mapas para guardar
	public static Hashtable<String,Object> variables = new Hashtable<String,Object>();
	public static Hashtable<String,Object> procedimientos = new Hashtable<String,Object>();
	private static Hashtable<String,Object> etiquetas = new Hashtable<String,Object>();
	
	public static Hashtable<String, Object> getEtiquetas() {
		return etiquetas;
	}

	public static void setEtiquetas(Hashtable<String, Object> etiquetas) {
		Helper.etiquetas = etiquetas;
	}

	public static void setPenDown(boolean pen)
	{
		penDown = pen;
	}
	public static void setPenColor(int color){
            
           switch(color){
               case 1: 
               penColor = Color.BLUE;
               System.out.println("Cambiando a color azul.");
               break;
                   
               case 2:
               penColor = Color.GREEN;
               System.out.println("Cambiando a color verde.");
               break;    
                   
               case 3:
               penColor = Color.RED;
               System.out.println("Cambiando a color rojo.");
               break;
                   
               case 4:
               penColor = Color.YELLOW;
               System.out.println("Cambiando a color amarillo.");
               break;
           }
            
        }
        public static void setPenSize(int size){
            
            penSize = size;
        }
        public static void setHeading(int degree){
            
            
        }
                
	public static Vector<Coordenadas> getCoordenadas() {
		return coordenadas;
	}
	public static int getTotalCoordenadas()
	{
		return coordenadas.size();
	}
	public static void limpiarCoordenadas() {
		Helper.coordenadas.clear();
		Helper.add(new Coordenadas(375,200));
	}

	public static Hashtable<String, Object> getVariables() {
		return variables;
	}
	public static void setVariables(Hashtable<String, Object> variables) {
		Helper.variables = variables;
	}
//	public static String searchVar(String key)
//	{
//		if (variables.get(key)!= null)
//			return variables.get(key).toString();
//		return "["+key+" variable not found]";
//	}
	public static Hashtable<String, Object> getFunciones() {
		return procedimientos;
	}
	public static void setProcedimientos(Hashtable<String, Object> procedimientos) {
		Helper.procedimientos = procedimientos;
	}
	
	public static Coordenadas add(Coordenadas p)
	{
		coordenadas.add(p);
		return p;
	}
	public static Coordenadas add(int dist)
	{
		Coordenadas added = new Coordenadas(Math.sin(Math.toRadians(180-angleMegaman))*Math.abs(dist), Math.cos(Math.toRadians(180-angleMegaman))*Math.abs(dist));
		added.setPenDown(penDown);
		if (coordenadas.size()>0)
			return add(new Coordenadas(coordenadas.get(coordenadas.size()-1).x+added.X(), coordenadas.get(coordenadas.size()-1).y+added.Y()));
		return added;
	}

	public static int getAngleMegaman() {
		return angleMegaman;
	}
	public static void setAngleMegaman(int angle) {
		Helper.angleMegaman = (Helper.angleMegaman+angle)%360;
//		System.out.println(Helper.angle);
	}
	
	public static String getOutput(){
		return output;
	}
	public static void setOutput(String out){
		output+=" "+out;
	}
	public static void clearOutput(){output = "";}
	@SuppressWarnings("unchecked")
	public static Color getColor(String var)
	{
		if (variables.get(var)!= null)
		{
    		if (variables.get(var)instanceof Vector)
    		{
    			Vector<String> lista = (Vector<String>)(variables.get(var));
    			if (lista.size()>=3)
    			{
    				try{
    					int R = Integer.parseInt(lista.get(0).trim());
    					int G = Integer.parseInt(lista.get(1).trim());
    					int B = Integer.parseInt(lista.get(2).trim());
    					if(R>=0 && R<=255 && G>=0 && G<=255 &&B>=0 &&B<=255)
    						return new Color(R,G,B);
    					else
    						setOutput("Error: La lista no contiene números válidos");
    				}catch(Exception e){Helper.setOutput("Error: La lista no contiene números");}
    			}
    			else
    			Helper.setOutput("Error: La lista no contiene suficientes elementos");
    		}
		}
		return Color.black;
	}
}
